package com.mud.ultimate.adapters;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mud.ultimate.R;
import com.mud.ultimate.data.GameRecord;

public class HighScoresAdapter extends BaseAdapter {

	private List<GameRecord> gameRecordList;
	private Context context;
	
	
	static class HighScoresViewHolder{
		TextView placeTV;
		TextView nameTV;
		TextView scoreTV;
		TextView dateTV;
		
	}
	
	public HighScoresAdapter(Context context, List<GameRecord> gameRecordList)
	{
		this.context = context;
		this.gameRecordList = gameRecordList;
		
	}
	
	
	@Override
	public int getCount() {
		return gameRecordList.size();
	}

	@Override
	public Object getItem(int index) {
		return gameRecordList.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {
		View v = convertView;
		if(v==null)
		{
			LayoutInflater inflater = LayoutInflater.from(context);
			v = inflater.inflate(R.layout.highscores_listitem, null);
			HighScoresViewHolder holder = new HighScoresViewHolder();
			holder.placeTV = (TextView) v.findViewById(R.id.highScorePlaceTV);
			holder.nameTV = (TextView) v.findViewById(R.id.highScoreNameTV);
			holder.scoreTV = (TextView) v.findViewById(R.id.highScoreScoreTV);
			holder.dateTV = (TextView) v.findViewById(R.id.highScoreDateTV);
			v.setTag(holder);
		}
		
		GameRecord record = gameRecordList.get(index);
		if(record!=null)
		{
			HighScoresViewHolder holder = (HighScoresViewHolder) v.getTag();
			holder.placeTV.setText(String.valueOf(index+1));
			holder.nameTV.setText(record.getName());
			holder.scoreTV.setText(String.valueOf(record.getScore()));
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			holder.dateTV.setText(formatter.format(record.getDate()));
			
		}
		
		return v;
	}

}
